package com.ly.hotel.spainterface.nm;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.spainterface.nm.bean.NmXFItemBean;
import com.ly.hotel.spainterface.nm.bean.NmXFTypeBean;
import com.ly.hotel.spainterface.nm.event.XFUpdateItemHideResEvent;
import com.ly.hotel.spainterface.nm.event.XFUpdateItemSortEvent;
import com.ly.hotel.spainterface.nm.event.XFUpdateTypeHideResEvent;
import com.ly.hotel.spainterface.nm.event.XFUpdateTypeSortEvent;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by SHUBEN on 2018/3/19.
 */

public class StaffOperationDialog extends Dialog {
    private NmXiaoFeiActivity mcontext;
    private MyApplication app;
    private ListView mTypeList;
    private ListView mItemList;
    private NmXFOperTypeAdapter nmXFOperTypeAdapter;
    private NmXFOperItemAdapter nmXFOperItemAdapter;
    private List<NmXFTypeBean> mTl = new ArrayList<NmXFTypeBean>();
    private NmXFTypeBean selectBean;
    private List<NmXFItemBean> mIl = new ArrayList<NmXFItemBean>();
    private String typeSortArr;
    private String itemSortArr;
    private Handler handler = new Handler(){
        public void handleMessage(Message msg){
            switch (msg.what){
                case 1:
                    mcontext.setTypeSortValue(typeSortArr);
                    break;
                case 2:
                    mcontext.setItemSortValue(itemSortArr,selectBean.getId());
                    break;
                default:
                    break;
            }
        }
    };

    public StaffOperationDialog(NmXiaoFeiActivity context, int theme) {
        super(context, theme);
        mcontext = context;
        selectBean = mcontext.getSelectBean();
        updateItemSort();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        app = (MyApplication) mcontext.getApplicationContext();
        EventBus.getDefault().register(this);
        setContentView(R.layout.nm_xf_operation);

        mTypeList = (ListView) findViewById(R.id.oper_type_list);
        mItemList = (ListView) findViewById(R.id.oper_item_list);
        mTl.addAll(app.nmXFTypeList);
        updateMIL();
        mTypeList.setItemsCanFocus(true);
        mItemList.setItemsCanFocus(true);

        Button backBtn = (Button) findViewById(R.id.dialog_back);
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                dismiss();
            }
        });
        setTypeAdap();
        setItemAdap();
    }

    private void setTypeAdap(){
        nmXFOperTypeAdapter = new NmXFOperTypeAdapter(mcontext,mTl);
        setSelectionId();
        mTypeList.setAdapter(nmXFOperTypeAdapter);
    }

    private void setSelectionId(){
        if(mTl.size() > 0 && selectBean != null) {
            String sId = selectBean.getId();
            for (int i = 0; i < mTl.size(); i++) {
                if (mTl.get(i).getId().equals(sId)) {
                    nmXFOperTypeAdapter.setSelectedPosition(i);
                    break;
                }
            }
        }
    }

    private void setItemAdap(){
        nmXFOperItemAdapter = new NmXFOperItemAdapter(mcontext,mIl);
        mItemList.setAdapter(nmXFOperItemAdapter);
    }


    //类型隐藏成功后刷新界面
    public void onEventMainThread(XFUpdateTypeHideResEvent e) {
        updateTypeView();
    }

    //项目隐藏成功后刷新界面
    public void onEventMainThread(XFUpdateItemHideResEvent e) {
        updateItemView();
    }

    //更新项目
    private void updateMIL(){
        mIl.clear();
        if(selectBean != null && selectBean.getTypeItems() != null) {
            mIl.addAll(selectBean.getTypeItems());
        }
    }

    private void updateTypeView(){
        mTl.clear();
        mTl.addAll(app.nmXFTypeList);
        updateTypeSort();
    }

    private void updateTypeSort(){
        if(TextUtils.isEmpty(mcontext.getTypeSortArr())){
            typeSortArr = "[]";
        }else {
            typeSortArr = new String(mcontext.getTypeSortArr());
        }
    }

    private void updateItemSort(){
        if(selectBean == null || TextUtils.isEmpty(selectBean.getSortItems())){
            itemSortArr = "[]";
        }else {
            itemSortArr = new String(selectBean.getSortItems());
        }
    }

    private  void updateItemView(){
        selectBean = mcontext.getSelectBean();
        updateItemSort();
        setSelectionId();
        nmXFOperTypeAdapter.notifyDataSetChanged();
        updateMIL();
        nmXFOperItemAdapter.notifyDataSetChanged();
    }

    //接收商品排序事件 XFUpdateTypeSortEvent
    public void onEventMainThread(XFUpdateTypeSortEvent e) {
        if(handler.hasMessages(1)){
            handler.removeMessages(1);
        }
        String id = e.getTypeBean().getId();
        if(!TextUtils.isEmpty(typeSortArr)) {
            try {
                JSONArray jsonArray = new JSONArray(typeSortArr);
                JSONArray jsonArray1 = new JSONArray();
                jsonArray1.put(id);
                for (int i = 0; i < jsonArray.length(); i++) {
                    String aid = jsonArray.getString(i);
                    if (!aid.equals(id)) {
                        jsonArray1.put(aid);
                    }
                }
                typeSortArr = jsonArray1.toString();
            } catch (JSONException el) {

            }
        }else {
            JSONArray jsonArray = new JSONArray();
            jsonArray.put(id);
            typeSortArr = jsonArray.toString();
        }
        try {
            JSONArray arr = new JSONArray(typeSortArr);
            for (int i = arr.length() -1; i >= 0; i--) {
                String lan = arr.getString(i);
                for (NmXFTypeBean b : mTl) {
                    if(lan.equals(b.getId())) {
                        mTl.remove(b);
                        mTl.add(0,b);
                        break;
                    }
                }
            }
            nmXFOperTypeAdapter.notifyDataSetChanged();
        }catch (JSONException ea){
            Toast.makeText(mcontext,"提交失败！",Toast.LENGTH_SHORT).show();
            updateTypeView();
        }
        handler.sendEmptyMessageDelayed(1,1500);
    }

    //项目排序事件操作
    public void onEventMainThread(XFUpdateItemSortEvent e) {
        if(handler.hasMessages(2)){
            handler.removeMessages(2);
        }
        String id = e.getItemBean().getId();
        if(!TextUtils.isEmpty(itemSortArr)) {
            try {
                JSONArray jsonArray = new JSONArray(itemSortArr);
                JSONArray jsonArray1 = new JSONArray();
                jsonArray1.put(id);
                for (int i = 0; i < jsonArray.length(); i++) {
                    String aid = jsonArray.getString(i);
                    if (!aid.equals(id)) {
                        jsonArray1.put(aid);
                    }
                }
                itemSortArr = jsonArray1.toString();
            } catch (JSONException el) {

            }
        }else {
            JSONArray jsonArray = new JSONArray();
            jsonArray.put(id);
            itemSortArr = jsonArray.toString();
        }
        try {
            JSONArray arr = new JSONArray(itemSortArr);
            for (int i = arr.length() -1; i >= 0; i--) {
                String lan = arr.getString(i);
                for (NmXFItemBean b : mIl) {
                    if(lan.equals(b.getId())) {
                        mIl.remove(b);
                        mIl.add(0,b);
                        break;
                    }
                }
            }
            nmXFOperItemAdapter.notifyDataSetChanged();
        }catch (JSONException ea){
            Toast.makeText(mcontext,"提交失败！",Toast.LENGTH_SHORT).show();
            updateItemView();
        }
        handler.sendEmptyMessageDelayed(2,1500);
    }


    @Override
    protected void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }
}